/**
 * @author hello
 */

$(document).ready(function(){
    $('#register_huozhu').click(function() {
        $('#register_huodai,#register_chuandai,#register_chedui,#register_siji').removeClass('active');
        $('#register_huozhu').addClass('active');
        $('#div-huozhu').show(500);
        $('#div-huodai,#div-chuandai,#div-chedui,#div-siji').hide();
    });
    $('#register_huodai').click(function() {
        $('#register_huozhu,#register_chuandai,#register_chedui,#register_siji').removeClass('active');
        $('#register_huodai').addClass('active');
        $('#div-huodai').show(500);
        $('#div-huozhu,#div-chuandai,#div-chedui,#div-siji').hide();
    });
    $('#register_chuandai').click(function() {
        $('#register_huodai,#register_huozhu,#register_chedui,#register_siji').removeClass('active');
        $('#register_chuandai').addClass('active');
        $('#div-chuandai').show(500);
        $('#div-huodai,#div-huozhu,#div-chedui,#div-siji').hide();
    });
    $('#register_chedui').click(function() {
        $('#register_huodai,#register_chuandai,#register_huozhu,#register_siji').removeClass('active');
        $('#register_chedui').addClass('active');
        $('#div-chedui').show(500);
        $('#div-huodai,#div-chuandai,#div-huozhu,#div-siji').hide();
    });
    $('#register_siji').click(function() {
        $('#register_huodai,#register_chuandai,#register_chedui,#register_huozhu').removeClass('active');
        $('#register_siji').addClass('active');
        $('#div-siji').show(500);
        $('#div-huodai,#div-chuandai,#div-chedui,#div-huozhu').hide();
    });
    function checkName(name){
    	if(name && checkLength(name,6,30))
    		return true;
    	else 
    		return false;
    }
    function checkPassword(password){
    	if(password && checkLength(password,6,10))
    		return true;
    	else
    		return false;
    }
    function checkPasswordAgain(password1,password2){
    	if(password1 && password2 && (password1 == password2))
    		return true;
    	else
    		return false;
    }
    function checkEmail(email){
    	if(email && /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email))
    		return true;
    	else
    		return false;
    }
    function checkPhone(phone){
    	if(phone && (/^[0-9]{3,4}\-\d{7,8}(\(\d{1,6}\))?$/.test(phone) || 
    			/(^0{0,1}1[3|4|5|6|7|8|9][0-9]{9}$)/.test(phone)))
    		return true;
    	else
    		return false;
    }
    function checkDescription(description){
    	if(description)
    		return true;
    	else
    		return false;
    }
    function trim(str){
    	if(str != null)
 　　           	return str.replace(/(^\s*)|(\s*$)/g, "");
    	else 
    		return str;
 　    }
    function checkLength(o,min,max){
    	if(o.length > max || o.length < min){
    		return false;
    	}else{
    		return true;
    	}
   }
    
    $('#shipper_btn').click(function(){
    	var name = trim($('#shipper_name').val()),
    		password = trim($('#shipper_password').val()),
    		email = trim($('#shipper_email').val()),
    		phone = trim($('#shipper_phone').val()),
    		password2 = trim($('#shipper_password2').val());
    	
    	$('#shipper_name_help').html('');
    	$('#shipper_password_help').html('');
    	$('#shipper_password2_help').html('');
    	$('#shipper_email_help').html('');
    	$('#shipper_phone_help').html('');
    	
    	if(!checkName(name))
    		$('#shipper_name_help').html('length must be between 6 and 30');
    	else if(!checkPassword(password))
    		$('#shipper_password_help').html('length must be between 6 and 10');
    	else if(!checkPasswordAgain(password,password2))
    		$('#shipper_password2_help').html('password wrong');
    	else if(!checkEmail(email))
    		$('#shipper_email_help').html('email must be like example@you.com');
    	else if(!checkPhone(phone))
    		$('#shipper_phone_help').html('wrong phone number');
    	else
    		$.ajax({
				type:'POST',
				url: 'register',
				dataType: "json",
				data: {"email":email,"name":name,"password":password,"phone":phone,"role":"Shipper"},			
				success:function(json){
						if(json.success != 1){
							$('#shipper_email_help').html(' email has existed!');
						}else{
							$('#shipper_form').submit();
						}
				}
    		});
    })
    
    $('#shipagent_btn').click(function(){
	    var name = trim($('#shipagent_name').val()),
			password = trim($('#shipagent_password').val()),
			email = trim($('#shipagent_email').val()),
			phone = trim($('#shipagent_phone').val()),
			password2 = trim($('#shipagent_password2').val()),
	    	description = trim($('#shipagent_description').val());
		
		$('#shipagent_name_help').html('');
		$('#shipagent_password_help').html('');
		$('#shipagent_password2_help').html('');
		$('#shipagent_email_help').html('');
		$('#shipagent_phone_help').html('');
		$('#shipagent_description_help').html('');
		
		if(!checkName(name))
			$('#shipagent_name_help').html('length must be between 6 and 30');
		else if(!checkPassword(password))
			$('#shipagent_password_help').html('length must be between 6 and 10');
		else if(!checkPasswordAgain(password,password2))
			$('#shipagent_password2_help').html('password wrong');
		else if(!checkEmail(email))
			$('#shipagent_email_help').html('email must be like example@you.com');
		else if(!checkPhone(phone))
			$('#shipagent_phone_help').html('wrong phone number');
		else if(!checkDescription(description))
			$('#shipagent_description_help').html('description null');
		else
			$.ajax({
				type:'POST',
				url: 'register',
				dataType: "json",
				data: {"email":email,"name":name,"password":password,"phone":phone,"description":description,"role":"ShippingAgent"},		
				success:function(json){
						if(json.success != 1){
							$('#shipagent_email_help').html(' email has existed!');
						}else{
							$('#shippingagents_form').submit();
						}
				}
    		});
    })
    
    $('#liner_btn').click(function(){
	    var name = trim($('#liner_name').val()),
			password = trim($('#liner_password').val()),
			email = trim($('#liner_email').val()),
			phone = trim($('#liner_phone').val()),
			password2 = trim($('#liner_password2').val()),
	    	description = trim($('#liner_description').val());
		
		$('#liner_name_help').html('');
		$('#liner_password_help').html('');
		$('#liner_password2_help').html('');
		$('#liner_email_help').html('');
		$('#liner_phone_help').html('');
		$('#liner_description_help').html('');
		
		if(!checkName(name))
			$('#liner_name_help').html('length must be between 6 and 30');
		else if(!checkPassword(password))
			$('#liner_password_help').html('length must be between 6 and 10');
		else if(!checkPasswordAgain(password,password2))
			$('#liner_password2_help').html('password wrong');
		else if(!checkEmail(email))
			$('#liner_email_help').html('email must be like example@you.com');
		else if(!checkPhone(phone))
			$('#liner_phone_help').html('wrong phone number');
		else if(!checkDescription(description))
			$('#liner_description_help').html('description null');
		else
			$.ajax({
				type:'POST',
				url: 'register',
				dataType: "json",
				data: {"email":email,"name":name,"password":password,"phone":phone,"description":description,"role":"LinerAgent"},
				success:function(json){
						if(json.success != 1){
							$('#liner_email_help').html(' email has existed!');
						}else{
							$('#liner_form').submit();
						}
				}
    		});
    })
    
    $('#moto_btn').click(function(){
	    var name = trim($('#moto_name').val()),
			password = trim($('#moto_password').val()),
			email = trim($('#moto_email').val()),
			phone = trim($('#moto_phone').val()),
			password2 = trim($('#moto_password2').val()),
	    	description = trim($('#moto_description').val());
		
		$('#moto_name_help').html('');
		$('#moto_password_help').html('');
		$('#moto_password2_help').html('');
		$('#moto_email_help').html('');
		$('#moto_phone_help').html('');
		$('#moto_description_help').html('');
		
		if(!checkName(name))
			$('#moto_name_help').html('length must be between 6 and 30');
		else if(!checkPassword(password))
			$('#moto_password_help').html('length must be between 6 and 10');
		else if(!checkPasswordAgain(password,password2))
			$('#moto_password2_help').html('password wrong');
		else if(!checkEmail(email))
			$('#moto_email_help').html('email must be like example@you.com');
		else if(!checkPhone(phone))
			$('#moto_phone_help').html('wrong phone number');
		else if(!checkDescription(description))
			$('#moto_description_help').html('description null');
		else
			$.ajax({
				type:'POST',
				url: 'register',
				dataType: "json",
				data: {"email":email,"name":name,"password":password,"phone":phone,"description":description,"role":"Motorcade"},
				success:function(json){
						if(json.success != 1){
							$('#moto_email_help').html(' email has existed!');
						}else{
							$('#motorcades_form').submit();
						}
				}
    		});
    })
    
    $('#driver_btn').click(function(){
	    var name = trim($('#driver_name').val()),
			password = trim($('#driver_password').val()),
			email = trim($('#driver_email').val()),
			phone = trim($('#driver_phone').val()),
			password2 = trim($('#driver_password2').val()),
			moto = trim($('#driver_moto').val());
		
		$('#driver_name_help').html('');
		$('#driver_password_help').html('');
		$('#driver_password2_help').html('');
		$('#driver_email_help').html('');
		$('#driver_phone_help').html('');
		$('#driver_moto_help').html('');
		
		if(!checkName(name))
			$('#driver_name_help').html('length must be between 6 and 30');
		else if(!checkPassword(password))
			$('#driver_password_help').html('length must be between 6 and 10');
		else if(!checkPasswordAgain(password,password2))
			$('#driver_password2_help').html('password wrong');
		else if(!checkEmail(email))
			$('#driver_email_help').html('email must be like example@you.com');
		else if(!checkPhone(phone))
			$('#driver_phone_help').html('wrong phone number');
		else if(!checkEmail(moto))
			$('#driver_moto_help').html('email must be like example@you.com');
		else
			$.ajax({
				type:'POST',
				url: 'register',
				dataType: "json",
				data: {"email":email,"name":name,"password":password,"phone":phone,"moto":moto,"role":"Driver"},	
				success:function(json){
						if(json.success != 1){
							$('#driver_email_help').html(' email has existed!');
						}else{
							$('#drivers_form').submit();
						}
				}
    		});
    })
})
